<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
	<link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>

<div id="updateCommodityDiv">
	<form class="layui-form" id="editCustomer" lay-filter="editCustomer">
		<div class="layui-panel">
			<div class="layui-form-item">
				<label class="layui-form-label">商品编号:</label>
				<div class="layui-input-inline">
					<input type="text" name="proId" autocomplete="off" readonly="" class="layui-input"
						   style="border:none; outline:medium;">
				</div>

				<label class="layui-form-label">商品名称:</label>
				<div class="layui-input-inline">
					<input type="text" name="proName" autocomplete="off" readonly="" class="layui-input"
						   style="border:none; outline:medium;">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">规格型号:</label>
				<div class="layui-input-inline">
					<input type="text" name="proSize" autocomplete="off" readonly="" class="layui-input"
						   style="border:none;">
				</div>
				<label class="layui-form-label">单位:</label>
				<div class="layui-input-inline">
					<input type="text" name="unitName" autocomplete="off" readonly="" class="layui-input"
						   style="border:none; outline:medium;">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">当前库存:</label>
				<div class="layui-input-inline">
					<input type="text" name="inveDescInventoryNum" autocomplete="off" readonly=""
						   class="layui-input" style="border:none; outline:medium;">
				</div>
				<label class="layui-form-label">批准文号:</label>
				<div class="layui-input-inline">
					<input type="text" name="proPostil" autocomplete="off" readonly="" class="layui-input"
						   style="border:none; outline:medium;">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">生产厂商:</label>
				<div class="layui-input-inline">
					<input type="text" name="manufacturerName" autocomplete="off" readonly=""
						   class="layui-input" style="border:none; outline:medium;">
				</div>
				<label class="layui-form-label">备注:</label>
				<div class="layui-input-inline">
					<input type="text" name="proComment" autocomplete="off" readonly="" class="layui-input"
						   style="border:none; outline:medium;">
				</div>
			</div>
		</div>
		<div class="layui-panel">
			<div class="layui-form-item">
				<label class="layui-form-label">参考进价:</label>
				<div class="layui-input-inline">
					<input type="text" name="proBid" autocomplete="off" class="layui-input">
				</div>
				<label class="layui-form-label">数量:</label>
				<div class="layui-input-inline">
					<input type="text" name="purStoDescNum" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">产品批号:</label>
				<div class="layui-input-inline">
					<input type="text" name="inveDescBatchNum" autocomplete="off" class="layui-input">
				</div>
				<label class="layui-form-label">总金额:</label>
				<div class="layui-input-inline">
					<input type="text" name="totalMoney" readonly autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item" style="display: none;">
				<input type="txt" name="proSalePrice" autocomplete="off" class="layui-input">
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">生产日期:</label>
				<div class="layui-input-inline">
					<input type="txt" name="inveDescStartDate" id="productionDate" autocomplete="off"
						   class="layui-input">
				</div>
				<label class="layui-form-label">有效期至:</label>
				<div class="layui-input-inline">
					<input type="txt" name="inveDescValidityDate" id="effectiveDate" readonly autocomplete="off"
						   class="layui-input">
				</div>
			</div>
			<input type="hidden" name="proKeepDate"autocomplete="off"class="layui-input">
			<div class="layui-form-item">
				<label class="layui-form-label">备注</label>
				<div class="layui-input-block">
					<input type="txt" name="inveDescComment" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item" style="text-align: center;">
				<button class="layui-btn layui-btn-sm" lay-submit lay-filter="btn">确定</button>
			</div>
		</div>

	</form>
	<div class="layui-panel">
		<fieldset class="tatable-search-fieldset" style="border:1px solid #eee;">

			<legend>采购历史</legend>
			<div> 该商品采购进货历史</div>
			<table class="layui-hide" id="purchaseHistory" lay-filter="purchaseHistory"></table>
		</fieldset>
	</div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'tree', 'tableSelect'], function() {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            tableSelect = layui.tableSelect,
            tree = layui.tree;
        var laydate = layui.laydate;

        var proKeepDate="";

        //时间控件
        laydate.render({
            elem: '#productionDate',
            value: new Date(),
            isInitValue: true,
            done: function(value, date, endDate){
                console.log(proKeepDate)
                    $("input[name=inveDescValidityDate]").val(countEndDate(value,proKeepDate));
            }

        });

        //获取父页面商品信息(String对象 --> JSON格式的对象)
        var dataobj = JSON.parse(parent.purchaseDetailsInfo);

        //4.根据生产日期和保质期计算出截止日期方法
        function countEndDate(beginDate, day) {
            if( (day % 12) == 0 ){
                var baoYear = day / 12;
                day = 365*baoYear;
            }else{
                day = day*30;
            }
            //转换日期，用于计算
            var beginTime = Date.parse(beginDate);
            //计算出截止日期
            var times = beginTime + 3600000*24*day;

            //将截止日期转换成指定格式
            var endDate = changeDateFormat(times);

            return endDate;
        }

        //日期格式方法
        function changeDateFormat(date) {
            var date = new Date(date);
            var year = date.getFullYear();
            var month = mendZero(date.getMonth()+1);
            var day = mendZero(date.getDate());
            return year + '-' + month + '-' + day;
        }

        //日期补0方法
        function mendZero(value) {
            var result = 0;
            if( value <= 9 ){
                result = '0' + (value);
            }else{
                result = value;
            }
            return result;
        }

        var proId = dataobj.proId;
        //历史进货
        table.render({
            elem: '#purchaseHistory	',
            url: '/searchPurchaseHistory?proId='+proId,
            cols: [
                [{
                    field: 'id',
                    title: '进货日期',
                    width:120,
                    templet:"<div>{{layui.util.toDateString(d.purchaseStock.purStoDate, 'yyyy-MM-dd')}}</div>"

                },
                    {
                        field: 'purStoDescId',
                        width:120,
                        title: '单据编号'
                    },
                    {
                        field: 'purStoDescPrice',
                        width:120,
                        title: '单价',
                        sort: true
                    },
                    {
                        field: 'purStoDescNum',
                        title: '数量',
                        width:120,
                    },
                    {
                        field: 'sign',
                        title: '合计金额',
                        templet:"<div>{{d.purStoDescPrice*d.purStoDescNum}}</div>",
                        width:120,

                    },
                    {
                        field: 'purStoDescStartDate',
                        width:120,
                        title: '生产日期',
                        templet:"<div>{{layui.util.toDateString(d.purStoDescStartDate, 'yyyy-MM-dd')}}</div>"
                    },
                    {
                        field: 'proBatchNum',
                        width:120,
                        title: '批号',
                        sort: true
                    },
                    {
                        field: 'purchaseProValidityDate',
                        width:120,
                        title: '有效期',
                        templet:"<div>{{layui.util.toDateString(d.purchaseProValidityDate, 'yyyy-MM-dd')}}</div>"
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: false,
            skin: 'line'
        });
        //历史进货列表单击行事件
        table.on('row(purchaseHistory)', function(obj) {
            //获取表格数据
            var purchaseHistoryobj = obj.data;
            //产品批号
            $("input[name=inveDescBatchNum]").val(purchaseHistoryobj.proBatchNum)
            //参考进价
            $("input[name=proBid]").val(purchaseHistoryobj.purStoDescPrice)
            //生产日期
            $("input[name=inveDescStartDate]").val(layui.util.toDateString(purchaseHistoryobj.purStoDescStartDate, 'yyyy-MM-dd'))
            //有效期
            $("input[name=inveDescValidityDate]").val(layui.util.toDateString(purchaseHistoryobj.purchaseProValidityDate, 'yyyy-MM-dd'))

        })

        var s =$("input[name=purStoDescNum]").val(1)
        if (parent.ifproductInfo == 'leftProduct') {
            $("input[name=inveDescValidityDate]").attr("readOnly", true);
            $("input[name=inveDescValidityDate]").val(countEndDate($("input[name=inveDescStartDate]").val(),dataobj.proKeepDate))
            proKeepDate=dataobj.proKeepDate;
            var myDate = new Date();
            var year = myDate.getYear(); //获取当前年
            var year = year < 2000 ? year + 1900 : year//计算2000后的具体年份
            var year = year.toString().substr(2, 2);//截取年份后两位
            var mon = myDate.getMonth() + 1; //获取当前月
            var date = myDate.getDate();
            if (mon<10){
                mon ='0'+mon;
            }
            if (date<10){
                date ='0'+date;
            }
            var purStoDate =(year+'-'+mon+'-'+date);
            var purchaseBatchNum=(year+mon+date);

            //产品批号
            $.ajax({
                url: '/searchProBatchNum/'+ proId + "/" + purStoDate,
                dataType:'json',
                type: 'get',
                success: function(data) {

                    if (data!=proId) {
                        $("input[name=inveDescBatchNum]").val(parseInt(data) + 1);
                    }else {
                        $("input[name=inveDescBatchNum]").val(purchaseBatchNum+'01');
                    }
                }
            });


            // 取出数据中第二层数据渲染给页面元素
            $("input[name=unitName]").val(dataobj.productUnit.unitName);

            if (null == dataobj.inventoryDesc) {
                $("input[name=inveDescInventoryNum]").val(0);
            } else {
                $("input[name=inveDescInventoryNum]").val(dataobj.inventoryDesc.inveDescInventoryNum);
            }
            $("input[name=manufacturerName]").val(dataobj.manufactureridInfo.manufacturerName);

        } else if (parent.ifproductInfo == 'rightProduct' || parent.ifproductInfo == 'centerProduct') {
            proKeepDate=dataobj.proKeepDate;
            $("input[name=proBid]").attr("readOnly", true)
            $("input[name=inveDescBatchNum]").attr("readOnly", true)
            $("input[name=inveDescStartDate]").attr("readOnly", true)
            $("input[name=inveDescValidityDate]").attr("readOnly", true)

        }else if (parent.ifproductInfo=='leftrecentStock'){
            //产品批号
            $("input[name=inveDescBatchNum]").val(dataobj.proBatchNum)
            //规格
            $("input[name=proSize]").val(dataobj.purchaseSize)
            //单位
            $("input[name=unitName]").val(dataobj.purStoDescUnit)
            //库存
            $("input[name=inveDescInventoryNum]").val(dataobj.inventoryDesc.inveDescInventoryNum)
            //批准文号
            $("input[name=proPostil]").val(dataobj.purPostil)
            //生产厂商
            $("input[name=manufacturerName]").val(dataobj.purStoDescManufacturer)
            //参考进价
            $("input[name=proBid]").val(dataobj.purStoDescPrice)
            //生产日期
            $("input[name=inveDescStartDate]").val(dataobj.purStoDescStartDate)
            //有效期
            $("input[name=inveDescValidityDate]").val(layui.util.toDateString(dataobj.purchaseProValidityDate, 'yyyy-MM-dd'))
            var s = parseInt($("input[name=purStoDescNum]").val())

            var beginTimes =$("input[name=inveDescStartDate]").val();
            var beginTimesa = $("input[name=inveDescValidityDate]").val();
             proKeepDate=(beginTimesa.substring(0,4)-beginTimes.substring(0,4))*12;
            $("input[name=totalMoney]").val(parseInt($("input[name=proBid]").val()) *s)
        }

        form.val('editCustomer', dataobj);
        //5.总金额自动计算
        //进价
        var buyPrice = $("input[name=proBid]");
        //数量
        var buyNum = $("input[name=purStoDescNum]").val(1);
        //初始进价总金额
        $("input[name=totalMoney]").val(parseInt(buyPrice.val())*buyNum.val());
        //按键被松开时触发
        buyPrice.on('keyup', function() {

            if (isNaN($(this).val())){
                layer.msg('请输入正确的数字！', {
                    time: 700
                })
                $(this).val(0);
            }

            checkNum($(this).val());
        });

        buyNum.on('keyup', function() {
            buyNum.val($(this).val());
            if (isNaN($(this).val())){
                layer.msg('请输入正确的数字！', {
                    time: 700
                })
            }
            checkNum($(this).val());
        });

        //限制input只能输入数字和两位小数
        function checkNum(num) {
            var isNum = isNaN(num);
            if (!isNum) {
                $("input[name=totalMoney]").val
                (+buyPrice.val() * +buyNum.val()) //总金额
            }
        }


        //表单提交按钮
        form.on('submit(btn)', function(data) {
            var Iflag = true;
            //tableContent用来保存表单数据
            tableContent = JSON.parse(JSON.stringify(data.field));
            //区分数据
            var lsArray = parent.tableContents;
            //区分重载页面
            var pages = 'chooseProductsId';
            //给tableContents表格实例集合追加赋值

            if (parent.ifproductInfo == 'centerProduct') {
                pages = 'currentTableId';
                lsArray = parent.currentTableList;
            }

            for (var i = 0, len = lsArray.length; i < len; i++) {
                if (lsArray[i].proId == tableContent.proId && lsArray[i].inveDescBatchNum == tableContent
                        .inveDescBatchNum ) {
                    if (parent.ifproductInfo == 'leftProduct') {
                        lsArray[i].purStoDescNum = parseInt(lsArray[i].purStoDescNum) + parseInt(
                            tableContent.purStoDescNum);
                    } else {
                        lsArray[i].purStoDescNum = parseInt(tableContent.purStoDescNum);
                    }
                    lsArray[i].totalMoney = lsArray[i].purStoDescNum * lsArray[i].proBid;
                    Iflag = false;
                    break;
                }
            }8

            if (Iflag) {
                lsArray.push(tableContent);
            }

            parent.layui.table.reload(pages, {
                //填充data属性
                data: lsArray
            })
            //关闭子页面
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        })

    })
</script>


</body>
</html>
